package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.News;
import org.example.mapper.NewsMapper;
import org.example.service.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author guocb
 * @since 2021-03-06
 */
//@CacheConfig(cacheNames = "newsList")
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {

    //缓存是在service层
    //2 重写增删改查方法
    //3 注解

//    @Cacheable(cacheNames = "newsList",key = "#wrapper.sqlSegment")
    @Override
    public List<News> list(Wrapper<News> wrapper) {
        return baseMapper.selectList(wrapper);
    }

    @Cacheable(cacheNames = "newsList")
    @Override
    public List<News> list(String t, String a) {
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        if (t != null && t != "") {
            queryWrapper.like(News.NTITLE, t);
        }
        if (a != null && a != "") {
            queryWrapper.eq(News.NAUTHOR, a);
        }
        return baseMapper.selectList(queryWrapper);
    }

//    @CacheEvict("newsList*")
//    @CachePut
}
